%include "I:\workdata\706727\Build\Directories_And_SampleRestrictions.sas";
%include "I:\workdata\706727\Build\Programs.sas";
libname dstform "\\srvfsenas1\data\Formater\SAS formater i Danmarks Statistik\SAS_datasaet\Sundhed";
libname exp "I:\Workdata\706727\Build\Data";

data bef (drop=befupdSourceYear);
set raw.befupdv (keep= pnr befupdSourceYear koen efalle kom IE_TYPE familie_Type civst CIV_VFRA BOP_VFRA FOED_DAG 
where=(befupdSourceyear>1999 and befupdSourceYear<2014 and befupdSourceyear-year(FOED_DAG)<=50 and befupdSourceyear-year(FOED_DAG)>10));
year=BEFupdSourceYear;
length year 8;
run;

/* Sample Full */
libname xptfile XPORT "I:\Workdata\706727\Build\Data\MLSevpnrs.xpt" access=readonly;
proc copy inlib=xptfile outlib=work memtype=data; run;

data statafe;
set MLSevpnr;
run;

%macro lagdata; 
  %do t=1 %to 5;
	  data base&t(drop= year rename=(year2=year));
	  set statafe;
	  year2=year-&t.;
	  run;
%end;
%mend lagdata;
%lagdata;

data basedata;
set %listsets(base,1,5);
run;

proc sort data=basedata nodup;
by pnr year ;
run;

proc sql;
create table mergespouse as select * from basedata as a
left join bef(keep=pnr year efalle) as b on a.pnr=b.pnr and a.year=b.year;
quit;

data basedata;
set mergespouse;
run;

/************************************************/
/************** Extra event data ****************/
/************************************************/

/** Moving and divorce **/
proc sort data=bef;
by pnr descending year;
run;

data bef(drop=BOP_VFRA  rename=(BOP_VFRA2=BOP_VFRA CIV_VFRA=lagCIV_VFRA CIV_VFRA2=CIV_VFRA CIVst=lagCIVst CIVst2=CIVst));
set bef;
if lag(pnr)=pnr then CIV_VFRA2=lag(CIV_VFRA);
if lag(pnr)=pnr then BOP_VFRA2=lag(BOP_VFRA);
if lag(pnr)=pnr then CIVst2=lag(CIVst);
run;

proc sql;
create table befvars as select * from basedata(drop=efalle) as a

left join bef(keep=pnr year BOP_VFRA  CIV_VFRA lagCIV_VFRA CIVst lagCIVst familie_Type) as b on a.pnr=b.pnr and a.year=b.year
order by pnr, year;
quit;

%macro lagspnr(start,end);
pnr1=lag(pnr);
	%do s=&start. %to &end.;
	%let j=%eval(&s-1);
	pnr&s.=lag(pnr&j.);
	%end;
%mend;

%macro lags(varlist,start,end);
%do i=1 %to %wordcount(&varlist.);
%let var=%scan(&varlist.,&i.);
if pnr=pnr1 then l1&var.=lag(&var.);
	%do s=&start. %to &end.;
	%let j=%eval(&s-1);
	if pnr=pnr&s. then l&s.&var.=lag(l&j.&var.);
	%end;
%end;
%mend;

data befvars(drop=pnr1 pnr2 pnr3 pnr4 pnr5 pnr6 BOP_VFRA CIV_VFRA lagCIV_VFRA lagCIVst moveyear);
set befvars;
by pnr;
moveyear=year(BOP_VFRA);
if year(BOP_VFRA)=year  then move=1;
if year(CIV_VFRA)=year  and (lagCIVst="G" or lagCIVst="P") and (CIVst ne "G" and CIVst ne "P" and CIVST ne "") then divorce=1;
%lagspnr(2,6);
%lags(divorce move,2,6);
if last.pnr then output;
run;

/* Family link and mental illness history */
data Links;
set raw.befupdv (keep=pnr mor_id far_id );
run;

proc sort data=links;
by pnr descending mor_id far_id;
run;

data links;
set links;
by pnr;
if first.pnr then output;
run;

data links;
set links;
rename mor_id=pnrm;
rename far_id=pnrf;
run;

/**** Deaths ****/
data dod1(keep=pnr d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate);
set raw.dodsaasgv(keep=pnr d_statdato C_DODTILGRUNDL_ACME dodsaasgsourceyear  rename=(C_DODTILGRUNDL_ACME=c_dod1 d_statdato=d_dodsdto) where=(dodsaasgsourceyear=2019));
diag4=substr(c_dod1,1,3);
if (substr(c_dod1,1,1)="C") then Dcancerdate=d_dodsdto;
if (substr(c_dod1,1,1)="I") then Dheartdate=d_dodsdto;
if (diag4="K70" or diag4="K71" or diag4="K72" or diag4="K73" or diag4="K74" or 
	  diag4="E10" or diag4="E11" or diag4="E12" or diag4="E13" or diag4="E14" or
	  diag4="J40" or diag4="J41" or diag4="J42" or diag4="J43" or diag4="J44" or substr(c_dod1,1,2)="C3") then Dbehadate=d_dodsdto;
if (substr(c_dod1,1,2)="G3") then Dotherserdate=d_dodsdto;
run;

data dod2(keep=pnr d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate);
set raw.dodsaarsv(keep=pnr d_dodsdto c_dod1);
diag4=substr(c_dod1,1,3);
if (substr(c_dod1,1,1)="C") then Dcancerdate=d_dodsdto;
if (substr(c_dod1,1,1)="I") then Dheartdate=d_dodsdto;
if (diag4="K70" or diag4="K71" or diag4="K72" or diag4="K73" or diag4="K74" or 
	  diag4="E10" or diag4="E11" or diag4="E12" or diag4="E13" or diag4="E14" or
	  diag4="J40" or diag4="J41" or diag4="J42" or diag4="J43" or diag4="J44" or substr(c_dod1,1,2)="C3") then Dbehadate=d_dodsdto;
if (substr(c_dod1,1,2)="G3") then Dotherserdate=d_dodsdto;
run;

data dod;
set dod1 dod2;
run;

/**** Hospital diagnoses ****/
data poppregco(keep= pnr d_inddto year cancer heart beha otherser birth abort pregother);
set raw.LPRadmv (keep=pnr recnum LPRadmSourceYear d_inddto c_adiag c_spec
where=(LPRadmSourceYear>1994 and c_spec ne "50" and c_spec ne "52"));
year=year(d_inddto);
diag4=substr(c_adiag,1,4);
cancer=(substr(c_adiag,1,2)="DC");
heart=(substr(c_adiag,1,2)="DI");
beha=(diag4="DK70" or diag4="DK71" or diag4="DK72" or diag4="DK73" or diag4="DK74" or 
	  diag4="DE10" or diag4="DE11" or diag4="DE12" or diag4="DE13" or diag4="DE14" or
	  diag4="DJ40" or diag4="DJ41" or diag4="DJ42" or diag4="DJ43" or diag4="DJ44" or substr(c_adiag,1,3)="DC3");
otherser=(substr(c_adiag,1,3)="DG3");
birth=(substr(c_adiag,1,3)="DO8");
abort=(substr(c_adiag,1,3)="DO0");
pregother=(substr(c_adiag,1,2)="DO" and birth=0 and abort=0);
run;

%macro max(varlist);
%do i=1 %to %wordcount(&varlist.);
%let var=%scan(&varlist.,&i.);
, max(&var.) as &var.
%end;
%mend;

proc sql;
create table preg as select pnr,year %max(birth abort pregother) from poppregco

group by pnr,year
order by pnr,year;
quit;

proc sort data=poppregco;
by pnr d_inddto;
run;

%macro createfirsts(diaglist);
%do i=1 %to %wordcount(&diaglist.);
%let diag=%scan(&diaglist.,&i.);

data &diag.(keep=pnr d_inddto rename=(d_inddto=&diag.date));
set poppregco(keep=where=(&diag.=1));
by pnr;
if first.pnr then output;
run;

proc sort data= &diag.;
by pnr;
run;

%end;
%mend createfirsts;

%createfirsts(cancer heart beha otherser);

data serdiags;
merge cancer heart beha otherser;
by pnr;
run;

/** Merge **/
%macro rename(varlist,end);
%do i=1 %to %wordcount(&varlist.);
%let var=%scan(&varlist.,&i.);
&var.=&var.&end
%end;
%mend;

proc sql;
create table basewlink as select * from basedata as a 

left join links as b on a.pnr=b.pnr;
quit;

/*** Pregnancy outcomes ***/
proc sql;
create table preglink as select * from basedata(keep=pnr efalle year rename=(efalle=pnry) where=(pnry ne "            ")) as a 

left join preg(keep=pnr year birth abort pregother) as b on a.pnr=b.pnr and a.year=b.year
left join preg(keep=pnr year birth abort pregother rename=(pnr=pnry birth=birthy abort=aborty pregother=pregothery)) as c on a.pnry=c.pnry and a.year=c.year 
order by pnr,year;
quit;

%macro lagspnr(start,end);
pnr1=lag(pnr);
	%do s=&start. %to &end.;
	%let j=%eval(&s-1);
	pnr&s.=lag(pnr&j.);
	%end;
%mend;

%macro lags(varlist,start,end);
%do i=1 %to %wordcount(&varlist.);
%let var=%scan(&varlist.,&i.);
if pnr=pnr1 then l1&var.=lag(&var.);
	%do s=&start. %to &end.;
	%let j=%eval(&s-1);
	if pnr=pnr&s. then l&s.&var.=lag(l&j.&var.);
	%end;
%end;
%mend;

data preglink(drop=pnry birthy aborty pregothery pnr1 pnr2 pnr3 pnr4 pnr5 pnr6);
set preglink;
birth=coalesce(birth,birthy);
abort=coalesce(abort,aborty);
pregother=coalesce(pregother,pregothery);
%lagspnr(2,6);
%lags(birth abort pregother,2,6);
run;

/** Child outcomes **/
proc sql;
create table varsf as select * from links as a 

left join dod as bf on a.pnr=bf.pnr
left join serdiags as cf on a.pnr=cf.pnr;
quit;

%macro listdata(varlist,end);
%do i=1 %to %wordcount(&varlist.);
%let var=%scan(&varlist.,&i.);
,&var.&end.
%end;
%mend;

proc sql;
create table childvars as select  * from basewlink as a 

left join varsf(drop=pnrf rename=(%rename(cancerdate heartdate behadate otherserdate d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate,cm))) as bm on a.pnr=bm.pnrm
left join varsf(drop=pnrm rename=(%rename(cancerdate heartdate behadate otherserdate d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate,cf))) as cm on a.pnr=cm.pnrf;
quit;

%macro dist(varlist);
%do i=1 %to %wordcount(&varlist.);
%let var=%scan(&varlist.,&i.);
&var.=coalesce(&var.cm,&var.cf);
dist&var.=year-year(&var.);
w1&var.=((dist&var.>0) and (dist&var.<1));
we&var.=(dist&var.>0);
%end;
%mend;

data childvars;
set childvars;
%dist(cancerdate heartdate behadate otherserdate d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate);
run;

%macro colchild(varlist);
%do i=1 %to %wordcount(&varlist.);
%let var=%scan(&varlist.,&i.);
, max(w1&var.) as w1&var.,  max(we&var.) as we&var.
%end;
%mend;

proc sql;
create table fchildvars as select distinct pnr,year
%colchild(cancerdate heartdate behadate otherserdate d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate) from childvars
group by pnr,year
order by pnr,year;
quit;


/* Merge */
proc sql;
create table mergefam as select a.pnr,a.year,a.pnry    %listdata(d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate cancerdate heartdate behadate otherserdate,)
														%listdata(d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate cancerdate heartdate behadate otherserdate,y)
														%listdata(d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate cancerdate heartdate behadate otherserdate,f)
														%listdata(d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate cancerdate heartdate behadate otherserdate,m)
from basewlink(rename=(efalle=pnry)) as a 

left join dod as b on a.pnr=b.pnr
left join serdiags as c on a.pnr=c.pnr

left join dod(rename=(%rename(d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate,y))) as by on a.pnry=by.pnr
left join serdiags(rename=(%rename(cancerdate heartdate behadate otherserdate,y))) as cy on a.pnry=cy.pnr

left join dod(rename=(%rename(d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate,m))) as bm on a.pnrm=bm.pnr
left join serdiags(rename=(%rename(cancerdate heartdate behadate otherserdate,m))) as cm on a.pnrm=cm.pnr

left join dod(rename=(%rename(d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate,f))) as bf on a.pnrf=bf.pnr
left join serdiags(rename=(%rename(cancerdate heartdate behadate otherserdate,f))) as cf on a.pnrf=cf.pnr;
quit;

%macro dist2(varlist,end);
%do i=1 %to %wordcount(&varlist.);
%let var=%scan(&varlist.,&i.);
dist&var.date&end.=year-year(&var.date&end.);
w1&var.date&end.=((dist&var.date&end.>0) and (dist&var.date&end.<1));
we&var.date&end.=(dist&var.date&end.>0);
%end;
%mend;

data mergefam2;
set mergefam;
%dist2(cancer heart beha otherser Dcancer Dheart Dbeha Dotherser,);
%dist2(cancer heart beha otherser Dcancer Dheart Dbeha Dotherser,y);
%dist2(cancer heart beha otherser Dcancer Dheart Dbeha Dotherser,m);
%dist2(cancer heart beha otherser Dcancer Dheart Dbeha Dotherser,f);
run;

proc sort data=mergefam2;
by pnr year;
run;

%macro listdum2(varlist,end);
%do i=1 %to %wordcount(&varlist.);
%let var=%scan(&varlist.,&i.);
,w1&var.date&end.,we&var.date&end.
%end;
%mend;

%macro rename2(varlist,end);
%do i=1 %to %wordcount(&varlist.);
%let var=%scan(&varlist.,&i.);
w1&var.=w1&var.&end. we&var.=we&var.&end.
%end;
%mend;

proc sql;
create table mergefamfinal as select a.pnr,a.year   %listdum2(cancer heart beha otherser Dcancer Dheart Dbeha Dotherser,)
													%listdum2(cancer heart beha otherser Dcancer Dheart Dbeha Dotherser,y)
													%listdum2(cancer heart beha otherser Dcancer Dheart Dbeha Dotherser,m)
													%listdum2(cancer heart beha otherser Dcancer Dheart Dbeha Dotherser,f)
													%listdum2(cancer heart beha otherser Dcancer Dheart Dbeha Dotherser,c) from mergefam2 as a

left join fchildvars(rename=(%rename2(cancerdate heartdate behadate otherserdate d_dodsdto Dcancerdate Dheartdate Dbehadate Dotherserdate,c))) as b on a.pnr=b.pnr and a.year=b.year;
quit;


/******************************************************/
/***************** Create dataset *********************/
/******************************************************/

/* Hospital */
%macro wordcount(list);
%local count;
%let count=0;
%do %while(%qscan(&list.,&count.+1,%str( )) ne %str() );
 %let count=%eval(&count.+1);
%end;
&count.
%mend wordcount;

%macro listdiags(diaglist,var);
%do i=1 %to %wordcount(&diaglist.);
%let diag=%scan(&diaglist.,&i.);
	%if &i<%wordcount(&diaglist.) %then
	&var.="&diag." or;
	%else &var.="&diag.";
%end;
%mend;

data pop(drop=LPRadmSourceYear);
set raw.LPRadmv (keep=pnr recnum LPRadmSourceYear d_inddto d_hendto c_adiag c_pattype c_spec c_indm c_henm v_sengdage
where=(LPRadmSourceYear>1999 and LPRadmSourceYear<2013 and c_spec ne "50" and c_spec ne "52"));
cont=1;
diag4=substr(c_adiag,1,4);
year=year(d_inddto);
diag5=cat(substr(diag4,1,4),substr(c_pattype,1,1));
run;

proc sql;
create table contlist as select diag5, sum(cont) as cont from pop

group by diag5
order by cont descending;
quit;

data contlist(keep=diag5 _Y);
set contlist(where=(cont>100000));
_Y=0;
run;

proc glmselect data=contlist noprint outdesign(addinputvars)=finalcolhosp(drop=_Y);
class diag5;
model _Y=diag5 /noint selection=none;
run;

data temp;
set finalcolhosp(drop=diag5);
run;

proc sql noprint;
select name into : varshosp separated by " " from dictionary.columns
where libname=upcase("work") and memname=upcase("temp");
quit; 
%put variabes=&vars.;

/* Medicine */
%macro meddata; 
  %do t=2000 %to 2012;
	data med&t(keep=pnr year med cont);
	set lmdb.lmdb&t.;
	med=substr(ATC,1,4);
	year=year(eksd);
	cont=1;
	run;
	
%end;
%mend meddata;
%meddata;

data fmeddata(where=(pnr ne ""));
set %listsets(med,2000,2012);
run;

proc sql;
create table medcol(where=(cont>100000)) as select distinct med,sum(cont) as cont from fmeddata
group by med
order by cont descending;
quit;

data medcol(keep=med _Y);
set medcol;
_Y=0;
run;

proc glmselect data=medcol noprint outdesign(addinputvars)=finalmedcol(drop=_Y);
class med;
model _Y=med /noint selection=none;
run;

data temp;
set finalmedcol(drop=med);
run;

proc sql noprint;
select name into : varsmed separated by " " from dictionary.columns
where libname=upcase("work") and memname=upcase("temp");
quit; 

%put &varsmed.;
%put &varshosp.;

/** Merge back on **/
%macro wordcount(list);
%local count;
%let count=0;
%do %while(%qscan(&list.,&count.+1,%str( )) ne %str() );
 %let count=%eval(&count.+1);
%end;
&count.
%mend wordcount;

%macro max(varlist);
%do i=1 %to %wordcount(&varlist.);
%let var=%scan(&varlist.,&i.);
, max(&var.) as &var.
%end;
%mend;

%macro createdumdata(dat,varsmac,dumdata,mvar);

	proc sql;
	create table merged as select * from basedata as a

	left join &dat.(drop=cont) as b on a.pnr=b.pnr and a.year=b.year;
	quit; 

	proc sql;
	create table mergeddum&dat. as select pnr,year %max(&varsmac.) from merged as a

	left join &dumdata.  as b on a.&mvar.=b.&mvar.

	group by pnr,year
	order by pnr,year; 
	quit;

%mend;

%createdumdata(fmeddata ,&varsmed.,finalmedcol,med);
%createdumdata(pop ,&varshosp.,finalcolhosp,diag5);

%macro lagspnr(start,end);
pnr1=lag(pnr);
	%do s=&start. %to &end.;
	%let j=%eval(&s-1);
	pnr&s.=lag(pnr&j.);
	%end;
%mend;

%macro lags(varlist,start,end);
%do i=1 %to %wordcount(&varlist.);
%let var=%scan(&varlist.,&i.);
if pnr=pnr1 then l1&var.=lag(&var.);
	%do s=&start. %to &end.;
	%let j=%eval(&s-1);
	if pnr=pnr&s. then l&s.&var.=lag(l&j.&var.);
	%end;
%end;
%mend;

data mergeddum;
merge mergeddumfmeddata mergeddumpop;
by pnr year;
run;

data mergeddum2(drop=pnr1 pnr2 pnr3 );
set mergeddum;
%lagspnr(2,3);
%lags(&varshosp. &varsmed.,2,3);
run; 

/***** Final Merge *****/
proc sql;
create table final as select * from statafe as a

left join mergeddum2 as b on a.pnr=b.pnr and a.year=b.year 
left join mergefamfinal as c on a.pnr=c.pnr and a.year=c.year 
left join preglink as d on a.pnr=d.pnr and a.year=d.year 
left join befvars as f on a.pnr=f.pnr and a.year=f.year ;
quit;

data final2;
set final;
array change _numeric_;
	do _i_=1 to dim(change);
	if change(_i_)=. then change(_i_)=0;
	end;
run;

proc sort data=final2 nodup;
by pnr year;
run;

/* Export as dta */

proc export data=final2
outfile = "J:\Workdata\706727\Temp\MLData_Sev_Full.dta"
replace;
run;


%put &varshosp.;
